package com.sto.stoapp.activity.administrator;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.SpinnerAdapter;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.entity.FunctionEntity;
import com.sto.stoapp.widget.StoHomeActivity;
import com.sto.stoapp.widget.StoSelectLinearLayout;

import java.util.ArrayList;
import java.util.List;

public class AdminAccessControlActivity extends StoHomeActivity {

	private Spinner programRoleSpinner, barRoleSpinner;

	private TextView programText, barAccessText;

	private StoSelectLinearLayout operatorControlLayout, orderFunctionLayout,
			vehicleManagerLayout, fastSearchLayout, receiveControl;

	private List<String> programList, roleList;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_access_control);
		this.setTitleBottom(R.string.access_control,
				HomeBroadAction.ADMINISTRATOR_BROAD);
		initWidget();
		initListener();
	}

	private void initListener() {
		operatorControlLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (operatorControlLayout.getSelectStatus()) {
					operatorControlLayout.setSelectStatus(false);
				} else {
					operatorControlLayout.setSelectStatus(true);
				}
				StoFilePreference.getInstance(AdminAccessControlActivity.this)
						.setSelectedOperatorControl(
								operatorControlLayout.getSelectStatus());
			}
		});
		orderFunctionLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (orderFunctionLayout.getSelectStatus()) {
					orderFunctionLayout.setSelectStatus(false);
				} else {
					orderFunctionLayout.setSelectStatus(true);
				}
				StoFilePreference.getInstance(AdminAccessControlActivity.this)
						.setOrderFunctionControl(
								orderFunctionLayout.getSelectStatus());
			}
		});
//		vehicleManagerLayout.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				if (vehicleManagerLayout.getSelectStatus()) {
//					vehicleManagerLayout.setSelectStatus(false);
//				} else {
//					vehicleManagerLayout.setSelectStatus(true);
//				}
//				StoFilePreference.getInstance(AdminAccessControlActivity.this)
//						.setVehicleManagerControl(
//								vehicleManagerLayout.getSelectStatus());
//			}
//		});

		fastSearchLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (fastSearchLayout.getSelectStatus()) {
					fastSearchLayout.setSelectStatus(false);
				} else {
					fastSearchLayout.setSelectStatus(true);
				}
				StoFilePreference.getInstance(AdminAccessControlActivity.this)
						.setExpressSearchControl(
								fastSearchLayout.getSelectStatus());
			}
		});

		receiveControl.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (receiveControl.getSelectStatus()) {
					receiveControl.setSelectStatus(false);
				} else {
					receiveControl.setSelectStatus(true);
				}
				StoFilePreference.getInstance(AdminAccessControlActivity.this)
						.setReceiveControl(receiveControl.getSelectStatus());
			}
		});
	}

	private void initWidget() {
		programRoleSpinner = (Spinner) findViewById(R.id.program_role_spinner);
		SpinnerAdapter programAdapter = new SpinnerAdapter(this,
				getProgramListRes());
		programRoleSpinner.setAdapter(programAdapter);
		programText = (TextView) findViewById(R.id.program_role_text);
		String programControl = StoFilePreference.getInstance(this)
				.getProgramRoleControl();
		programText.setText(programControl);
		programRoleSpinner
				.setOnItemSelectedListener(new ProgramSpinnerSelectedListener());
		programRoleSpinner
				.setSelection(selectedProgramPosition(programControl));

//		barRoleSpinner = (Spinner) findViewById(R.id.bar_role_spinner);
//		SpinnerAdapter barRoleAdapter = new SpinnerAdapter(this,
//				getRoleListRes());
//		barRoleSpinner.setAdapter(barRoleAdapter);
//		barAccessText = (TextView) findViewById(R.id.bar_role_text);
//		String roleControl = StoFilePreference.getInstance(this)
//				.getBarRoleControl();
//		barAccessText.setText(roleControl);
//		barRoleSpinner
//				.setOnItemSelectedListener(new RoleSpinnerSelectedListener());
//		barRoleSpinner.setSelection(selectedRolePosition(roleControl));

		operatorControlLayout = (StoSelectLinearLayout) findViewById(R.id.operator_control_layout);
		operatorControlLayout.setTitle(R.string.allow_select_operator);
		operatorControlLayout.setSelectStatus(StoFilePreference.getInstance(
				this).getSelectedOperatorControl());

		orderFunctionLayout = (StoSelectLinearLayout) findViewById(R.id.order_function_layout);
		orderFunctionLayout.setTitle(R.string.open_order_function);
		orderFunctionLayout.setSelectStatus(StoFilePreference.getInstance(this)
				.getOrderFunctionControl());
		orderFunctionLayout.setVisibility(View.GONE);

//		vehicleManagerLayout = (StoSelectLinearLayout) findViewById(R.id.vehicle_manager_layout);
//		vehicleManagerLayout.setTitle(R.string.vehicle_manager_function);
//		vehicleManagerLayout.setSelectStatus(StoFilePreference
//				.getInstance(this).getVehicleManagerControl());

		fastSearchLayout = (StoSelectLinearLayout) findViewById(R.id.fast_good_search_layout);
		fastSearchLayout.setTitle(R.string.fast_good_search_control);
		fastSearchLayout.setSelectStatus(StoFilePreference.getInstance(this)
				.getExpressSearchControl());

		receiveControl = (StoSelectLinearLayout) findViewById(R.id.receive_control);
		receiveControl.setTitle(R.string.receive_control);
		receiveControl.setSelectStatus(StoFilePreference.getInstance(this)
				.getReceiveControl());
	}

	private int selectedProgramPosition(String programControl) {
		int position = 0;
		if (programList == null) {
			return 0;
		}
		for (int i = 0; i < programList.size(); i++) {
			if (programControl.equals(programList.get(i))) {
				position = i;
			}
		}
		return position;
	}

	private int selectedRolePosition(String roleControl) {
		int position = 0;
		if (roleList == null) {
			return 0;
		}
		for (int i = 0; i < roleList.size(); i++) {
			if (roleControl.equals(roleList.get(i))) {
				position = i;
			}
		}
		return position;
	}

	private List<FunctionEntity> getRoleListRes() {
		roleList = new ArrayList<String>();
		roleList.add(getText(R.string.not_limit).toString());
		roleList.add(getText(R.string.access_sales).toString());
		roleList.add(getText(R.string.access_scan).toString());
		List<FunctionEntity> functionList = new ArrayList<FunctionEntity>();
		for (String string : roleList) {
			FunctionEntity entity = new FunctionEntity();
			entity.setTitle(string);
			functionList.add(entity);
		}
		return functionList;
	}

	private List<FunctionEntity> getProgramListRes() {
		programList = new ArrayList<String>();
		programList.add(getText(R.string.automatic).toString());
		programList.add(getText(R.string.site_point).toString());
		programList.add(getText(R.string.center_point).toString());
		programList.add(getText(R.string.aviation_point).toString());
		List<FunctionEntity> functionList = new ArrayList<FunctionEntity>();
		for (String string : programList) {
			FunctionEntity entity = new FunctionEntity();
			entity.setTitle(string);
			functionList.add(entity);
		}
		return functionList;
	}

	// 使用数组形式操作
	class ProgramSpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			TextView title = (TextView) arg1.findViewById(R.id.res_test);
			programText.setText(title.getText().toString());
			if (!StoFilePreference.getInstance(AdminAccessControlActivity.this)
					.getProgramRoleControl().equals(title.getText().toString())) {
				StoFilePreference.getInstance(AdminAccessControlActivity.this)
						.setProgramRoleControl(title.getText().toString());
				StoFilePreference.getInstance(AdminAccessControlActivity.this).setNeedLogin(true);
			} else {
				StoFilePreference.getInstance(AdminAccessControlActivity.this).setNeedLogin(true);
			}

		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}

	// 使用数组形式操作
	class RoleSpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			TextView title = (TextView) arg1.findViewById(R.id.res_test);
			barAccessText.setText(title.getText().toString());
			StoFilePreference.getInstance(AdminAccessControlActivity.this)
					.setBarRoleControl(title.getText().toString());
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}


}
